package cn.gson.fengboot.model.service;

import cn.gson.fengboot.model.dao.DempDao;
import cn.gson.fengboot.model.dao.DroleDao;
import cn.gson.fengboot.model.mapper.DempMapper;
import cn.gson.fengboot.model.pojos.Dbusiness;
import cn.gson.fengboot.model.pojos.QempFrom;
import cn.gson.fengboot.model.routervo.MypwdVo;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class DempService {

    @Autowired
    DempMapper dempMapper;

    @Autowired
    DempDao dempDao;

    @Autowired
    DroleDao droleDao;

    public List<QempFrom> selectmyemp(Integer roleId, Integer empId, String myempnameinput, Boolean myempservice, Boolean myempstatu) {
        String statu="";
        String empservice="";
        if(myempstatu){//如果为true我就查询所有员工含离职的
            statu="" ;
        }else{//否则只查询在职的
            statu="在职";
        }
        if(myempservice){//如果为true就查询只启用的
            empservice="启用";
        }else{//否则查询所有
            empservice="";
        }

        return dempMapper.selectmyemp(roleId,empId,myempnameinput,statu,empservice);
    }

    public Integer judgemyempname(String empName) {
        List<QempFrom> allByEmpName = dempDao.findAllByEmpName(empName);
        if(allByEmpName.size()>0){
            return 1;
        }else {
            return 0;
        }
    }

    public String addmyempfrom(String qempFrom) {
        JSONObject obj = (JSONObject)JSONObject.parse(qempFrom);
        QempFrom qemp=obj.getObject("empfrom",QempFrom.class);

        if(qemp.getEmpId()!=0&&qemp.getEmpId()!=null){//员工修改
            QempFrom qempFrom1 = dempDao.findById(qemp.getEmpId()).get();
            qemp.setEmpMima(qempFrom1.getEmpMima());
            dempDao.save(qemp);
            return "editsucceed";

        }else{//员工新增
            Integer judgemyempname = judgemyempname(qemp.getEmpName());
            if(judgemyempname==1){/*判断名称是否存在*/
                return "name";
            }else{
                qemp.setEmpSettime(new Timestamp(new Date().getTime()));
                qemp.setEmpStatu("在职");
                qemp.setEmpMima("123456");
                qemp.setEmpImgpath("personageimage/12345678.png");
                dempDao.save(qemp);
                return "succeed";
            }
        }

    }

    public QempFrom selectBymyempId(Integer empId) {
        return dempMapper.selectBymyempId(empId);
    }

    public String verifymypwd(MypwdVo mypwdVo) {
        String s = dempMapper.selectBypwdempId(mypwdVo.getEmpId());
        if(mypwdVo.getOldpwd().equals(s)){/*判断旧密码是否相符*/
            if(mypwdVo.getNewpwd().equals(mypwdVo.getTruenewpwd())){/*判断两次密码是否相符*/
                QempFrom qempFrom = dempDao.findById(mypwdVo.getEmpId()).get();
                qempFrom.setEmpId(mypwdVo.getEmpId());
                qempFrom.setEmpMima(mypwdVo.getNewpwd());
                dempDao.save(qempFrom);
                return "succeed";
            }else{
                return "two";
            }
        }else{
            return "old";
        }
    }


    /*保存用户头像*/
    public void personageimage(Integer empId,String imagepath){
        QempFrom qempFrom = dempDao.findById(empId).get();
        qempFrom.setEmpImgpath(imagepath);
        dempDao.save(qempFrom);
    }
}
